<?php

namespace Biz\model\activity;


class BizProjectGroupModel extends BizProjectBaseModel
{

    const _TABLE_NAME = 'biz_project_group';

    use BizProjectTrait;

    public static function getGroupIdsByAdminId($adminId): array
    {
        $list = self::dataSelect(['OR' => ['admin_id' => $adminId, 'group_id' => 0]] + self::statusCondition());
        $response = [];
        $master = [];
        foreach ($list as $v) {
            if ($v['admin_id'] == $adminId) {
                if ($v['group_id'] == 0) {
                    $groupId = $v['id'];
                    $master[] = $groupId;
                } else {
                    $groupId = $v['group_id'];
                }
                $response[] = $groupId;
            }
        }
        return [$response, $master];
    }

    public static function getGroupInfoByAdminId($adminId): array
    {
        $list = self::dataSelect(['OR' => ['admin_id' => $adminId, 'group_id' => 0]] + self::statusCondition());
        $response = [];
        $group = [];
        foreach ($list as $v) {
            if ($v['group_id'] == 0) {
                $group[$v['id']] = $v;
            }
            if ($v['admin_id'] == $adminId) {

                $groupId = $v['group_id'] ?: $v['id'];
                $response[] = &$group[$groupId];
            }
        }
        return array_filter($response);
    }

    public static function getGroupNameByAdminId($adminId): array
    {
        $list = self::dataSelect(['OR' => ['admin_id' => $adminId, 'group_id' => 0]] + self::statusCondition());
        $response = [];
        foreach ($list as $v) {
            if ($v['group_id'] == 0) {
                $group[$v['id']] = $v['name'];
            }
            if ($v['admin_id'] == $adminId) {
                $groupId = $v['group_id'] ?: $v['id'];
                $response[] = &$group[$groupId];
            }
        }
        return $response;
    }

    public static function getAuthAdminIdByAdminId($adminId): array
    {
        $response = [$adminId];
        //判断是否是leader
        $groupId = self::dataSelect(['admin_id' => $adminId, 'group_id' => 0] + self::statusCondition(), 'id');

        //查找所有责任方
        if ($groupId) {
            $adminId = self::dataSelect(['group_id' => $groupId] + self::statusCondition(), 'admin_id');
            if ($adminId) $response = array_merge($response, $adminId);
        }
        return $response;
    }

    public static function getLeaderIdByAdminId($adminId): array
    {
        $response = [];
        $groupId = $adminId ? self::dataSelect(['admin_id' => $adminId, 'group_id[!]' => 0] + self::statusCondition(), 'group_id') : [];
        if ($groupId) $response = self::dataSelect(['id' => $groupId] + self::statusCondition(), '*');
        return $response;
    }

    public static function getResponsibleLeader($responsible = null): array
    {
        $response = [];
        if (!is_null($responsible)) {
            $list = self::dataSelect(['responsible' => $responsible, 'group_id' => 0] + self::statusCondition(), '*');
            if ($list) {
                $response = $list;
            }
        }
        return $response;
    }

    public static function getAdminId(): array
    {
        $adminGroupIds = self::dataSelect(['responsible' => -1, 'group_id' => 0] + self::statusCondition(), 'id');
        $adminIds = [];
        if ($adminGroupIds) {
            $adminIds = self::dataSelect(['OR' => ['group_id' => $adminGroupIds, 'id' => $adminGroupIds]] + self::statusCondition(), 'admin_id');
        }
        return array_values(array_unique($adminIds));
    }

}
